/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.context.config;

import io.iec.edp.caf.commons.runtime.redis.properties.CafRedisConfiguration;
import io.iec.edp.caf.context.core.BizContextUpdateService;
import io.iec.edp.caf.context.core.listener.CafSessionExpiredEventListener;
import io.iec.edp.caf.context.dist.RedisBizContextManager;
import io.iec.edp.caf.context.memory.InMemoryBizContextManager;
import io.iec.edp.caf.core.context.BizContextBuilder;
import io.iec.edp.caf.core.context.BizContextManager;
import io.iec.edp.caf.core.context.ICAFContextService;
import io.iec.edp.caf.core.session.ICafSessionService;
import io.iec.edp.caf.multicontext.annotation.BeanCollector;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import java.util.List;

/**
 * This is {@link BusinessContextConfiguration}.
 *
 * @author yisiqi
 * @since 1.0.0
 */
@Configuration(proxyBeanMethods = false)
public class BusinessContextConfiguration {

    //处理caf-server问题(选择了方案1) 方案1：不让BizContextManager强依赖BizContextBuilder 方案2：底层提供默认BizContextBuilder实现
    @Bean
    @BeanCollector
    @ConditionalOnExpression("#{!'false'.equals(environment['redis.enabled'])}")
    public BizContextManager redisBizContextManager(ICAFContextService contextService, ICafSessionService sessionService, List<BizContextBuilder> contextBuilders, RedisConnectionFactory connectionFactory, CafRedisConfiguration cafRedisConfiguration) {
        return new RedisBizContextManager(contextService, sessionService, contextBuilders, connectionFactory, cafRedisConfiguration);
    }

    @Bean
    @BeanCollector
    @ConditionalOnMissingBean
    public BizContextManager memoryBizContextManager(ICAFContextService contextService, List<BizContextBuilder> contextBuilders) {
        return new InMemoryBizContextManager(contextService, contextBuilders);
    }

    @Bean
    public BizContextUpdateService defaultBizContextUpdateService(BizContextManager manager) {
        return new BizContextUpdateService(manager);
    }

    @Bean
    public CafSessionExpiredEventListener cafSessionExpiredEventListener(BizContextManager manager) {
        return new CafSessionExpiredEventListener(manager);
    }

}
